<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <!-- <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 订单表格</el-breadcrumb-item> -->
            </el-breadcrumb>
        </div>
        <div class="container">
            <!-- <span style="color: red"><strong>
                <p>批量删除</p>
                <p>模糊查询</p>
                <p>添加（待考察）</p>
            </strong></span> -->
          <!--   <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
                <el-select v-model="select_cate" placeholder="筛选条件" class="handle-select mr10">
                    <el-option key="1" label="姓名" value="name"></el-option>
                    <el-option key="2" label="电话" value="phone"></el-option>
                </el-select>
                <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
            </div> -->

            <el-table :data="data" border class="table" ref="multipleTable" @selection-change="handleSelectionChange">
               <!--  <el-table-column type="selection" width="55" align="center"></el-table-column> -->
                <el-table-column prop="end_time" label="截止有效期" sortable align="center" width="200">
                </el-table-column>
                <el-table-column prop="user.uname" label="姓名" align="center" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="user.uimg" label="头像"  align="center">

                <template slot-scope="scope">
                      <a :href="scope.row.user.uimg"><img :src="scope.row.user.uimg" alt="" style="width: 50px;height: 50px"></a>
                  </template>

              </el-table-column>
               <!--  <el-table-column prop="sex" label="性别" align="center" :formatter="fromSex">
                </el-table-column> -->
                <el-table-column prop="phone" label="电话" align="center" width="150">
                </el-table-column>
                <el-table-column prop="birthday" label="生日" align="center" width="150">
                </el-table-column>
                
                 <!-- <el-table-column prop="uid" label="地址" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-location-outline" @click="toAddress(scope.row.uid)">查看</el-button>
                    </template>
                </el-table-column> -->
               <!--  <el-table-column fixed='right' label="操作" align="center" width="150">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <div class="pagination">
                <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next" :total="total">
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
    <!--     <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form">

                <div class="editD">
                    <label class="editDL">姓名</label>
                    <el-input v-model="form.uname"></el-input>
                </div>
                 <div class="editD">
                    <label class="editDL">性别</label>
                    <div style="line-height: 2rem;">
                        <el-radio v-model="form.sex" label="1">男</el-radio>
                        <el-radio v-model="form.sex" label="2">女</el-radio>
                        <el-radio v-model="form.sex" label="3">未知</el-radio>
                    </div>
                        
                </div>
                 <div class="editD">
                     <label class="editDL">电话</label>
                    <el-input v-model="form.phone"></el-input>
                </div>
                <div class="editD">
                     <label class="editDL">生日</label>
                    <el-date-picker type="date" placeholder="选择日期" v-model="form.birthday" value-format="yyyy-MM-dd"></el-date-picker>
                </div>
                <div class="editD">
                       
                     <label class="editDL">头像</label>
                    <el-upload class="avatar-uploader" :action='$gretUrl+"admin/index/upimg"' :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                         <img v-if="form.uimg" :src="form.uimg" class="avatar">
                         <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                     </el-upload>
                </div>
                
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog> -->

        <!-- 删除提示框 -->
        <!-- <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog> -->
    </div>
</template>

<script>
    export default {
        inject : ['reload'],
        name: 'basetable',
        data() {
            return {
                // url: './static/vuetable.json',
                tableData: [],
                cur_page: 1,
                multipleSelection: [],
                select_cate: '',
                select_word: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                delVisible: false,
                form: {

                },
                idx: -1,
                admininfo:JSON.parse(localStorage.getItem('ms_username')),
                total:1,
                // imageUrl: '',
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.openid === this.del_list[i].openid) {
                            is_del = true;
                            break;
                        }
                    }
                    if (!is_del) {
                        if(d.openid.indexOf(this.select_word) > -1 ) {
                            return d;
                        }
                    }
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },

            // 获取 easy-mock 的模拟数据
            getData() {
                // 开发环境使用 easy-mock 数据，正式环境使用 json 文件
                // if (process.env.NODE_ENV === 'development') {
                //     this.url = '/ms/table/list';
                // };
                // this.$axios.get(this.url, {
                //     page: this.cur_page
                // }).then((res) => {
                //     this.tableData = res.data.list;
                // })
                // console.log(this.admininfo)
                  this.$axios.get('admin/member/purchase', {
                    params:{
                        aid: this.admininfo['admin_id'],
                        token: this.admininfo['token'],
                        page: this.cur_page
                    }
                }).then((res) => {
                    if(res.data.status == 0){
                            this.tableData = res.data.data.data;
                            this.total = res.data.data.total 
                        }else if(res.data.status == 3){
                           this.$router.push('login') 
                        }else{
                            this.$message.error(res.data.msg);
                        }
                })

            },
            search() {
                this.is_search = true;
            },
            formatter(row, column) {
                return row.address;
            },
            fromSex(row , column){
                switch(row.sex){
                    case '1':
                        return '男';
                        break;
                    case '2':
                        return '女';
                        break;
                    case '男':
                        return row.sex;
                        break;
                    case '女':
                        return row.sex;
                        break;
                    default:
                        return '未知';
                        break;
                }
            },
            filterTag(value, row) {
                return row.tag === value;
            },
            toAddress(uid) {
            
                this.$router.push({path:'/address',query:{uid:uid}});
            },
            handleEdit(index,row){
                this.idx = index;
                const item = this.tableData[index];
                if(item.sex == '男'){
                    item.sex = '1'
                }else if(item.sex == '女'){
                    item.sex = '2'
                }else{
                    item.sex = '3'
                }
                this.form = {
                    uid: item.uid,
                    // add_time:item.add_time,
                    uname: item.uname,
                    phone: item.phone,
                    uimg: item.uimg,
                    sex: item.sex,
                    birthday:item.birthday
                }
                // console.log(this.form);
                this.editVisible = true;
            },
            handleDelete(index, row) {
                this.idx = index;
                this.delVisible = true;
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            // 保存编辑
            saveEdit() {
                // console.log(this.form)
                // let data = this.form
               
                this.$axios.post('admin/user/userup',{
                    aid: this.admininfo.admin_id,
                    token: this.admininfo.token,
                    data: this.form
                })
                .then((res) =>{
                    // console.log(res);
                   
                    if(res.data.status == 0){
                        // console.log(this.tableData[this.idx].add_time)
                        // this.form.add_time =this.tableData[this.idx].add_time
                        // this.$set(this.tableData, this.idx, this.form);
                        // this.editVisible = false;
                        this.$message.success(`修改成功`);
                        this.reload()
                    }else{
                        this.$message(`修改失败`);
                    }
                    
                })
                // this.reload()
            },
            // 确定删除
            deleteRow(){

                // this.tableData.splice(this.idx, 1);
                // this.$message.success('删除成功');
                // this.delVisible = false;

                 this.$axios.post('admin/user/userdel',{
                    aid: this.admininfo.admin_id,
                    token: this.admininfo.token,
                    uid: this.tableData[this.idx].uid
                })
                .then((res) =>{

                    if(res.data.status == 0){
                        this.$message.success('删除成功');
                        this.reload()
                    }else{
                        this.$message('删除失败');
                    }
                })
                this.delVisible = false;


            },
            // 头像上传
             handleAvatarSuccess(res, file) {
                console.log(res);
                // this.imageUrl = URL.createObjectURL(file.raw);
                this.form.uimg = res.data.data.src
              },
              beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                  this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                  this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
              }
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .del-dialog-cnt{
        font-size: 16px;
        text-align: center
    }
    .table{
        width: 100%;
        font-size: 14px;
    }
    .red{
        color: #ff0000;
    }
    /*头像框*/
    .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
   /* width: 178px;
    height: 178px;*/
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    /*display: block;*/
  }

  /*排版*/
  .editD{
    display: flex; margin-bottom: 18px;
  }
  .editDL{
    flex: 0 0 100px;text-align: center;line-height: 32px;
  }
 /*.el-picker-panel.el-date-picker.el-popper{
    top:336px !important;
 }*/
</style>
